﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataVisit.Model;
using NHibernate.Transform;
using System.Collections;
using NHibernate;

namespace DataVisit.DAL.ReportData
{
    public class DProductReport:AbstractSupportDal<IProjectModel>
    {
        #region 产量收入分析
        //获取产量收入分析数据
        public IList GetData(string time)
        {
            try
            {
                IList list = Session.GetNamedQuery("GetProductData")
                    .SetDateTime("ctime", Convert.ToDateTime(time))
                    .SetResultTransformer(Transformers.AliasToEntityMap).List();
                return list;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region 材料市场
        //获取材料统计分析数据
        public IList GetMaterialsData(string time)
        {
            try
            {
                var sql=Session.GetNamedQuery("GetMaterialsData").QueryString.Replace("#tablename", HJReportName(time));
                sql = sql.Replace("#year", GetYear(time));
                sql = sql.Replace("#month", GetMonth(time));
                IList list = Session.CreateSQLQuery(sql)
                    .SetResultTransformer(Transformers.AliasToEntityMap).List();
                return list;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region 租赁市场
        //获取设备租赁资金
        public IList GetRentalPrice(string time)
        {
            try
            {
                IList list = null;
                this.Template.Execute(delegate(ISession session)
                {
                    list = session.GetNamedQuery("GetzlzjData")
                    .SetString("ctime", time)
                    .SetResultTransformer(Transformers.AliasToEntityMap).List();
                    return list;
                });

                return list;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
        //获取设备租赁数量
        public IList GetRentalCount(string time)
        {
            try
            {
                IList list = null;
                this.Template.Execute(delegate(ISession session) {
                    list = session.GetNamedQuery("GetzlslData")
                        .SetString("ctime", time)
                        .SetResultTransformer(Transformers.AliasToEntityMap).List();
                    return list;
                });

                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region 组合成为数据表
        private string YearReportName(string s)
        {
            return "R_2105_68_200_01_" + Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[0] + "_" + Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[1];
        }

        private string HJReportName(string s)
        {
            return "R_2105_68_003_300_" + Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[0] + "_" + Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[1];
        }
        //设备基本信息数据表
        private string EquipmentReportName(string s)
        {
            return "R_2105_24_10_1001_" + Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[0];
        }
        //获取年份
        private string GetYear(string s)
        {
            return Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[0];
        }
        //获取月份
        private string GetMonth(string s)
        {
            return Convert.ToDateTime(s).ToString("yyyy-MM-dd").Split('-')[1];
        }
        #endregion

        #region 设备基本信息和状态EquipmentReportName
        public int EquipmentCount(string time, string t, string c)
        {
            try
            {
                int list = 0;
                this.Template.Execute(delegate(ISession session)
                {
                    var sql = session.GetNamedQuery("GetEquipmentStateCount").QueryString.Replace("#tablename", EquipmentReportName(time));
                    sql = sql.Replace("#config", ConditionSQL(t, c));
                    list = session.CreateSQLQuery(sql)
                        .UniqueResult<int>();
                    return list;
                });
                return list;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        //获取设备状态等详细信息数据
        public IList GetEquipmentInfoData(string time,string t,string c,int size,int index)
        {
            try
            {
                IList list = null;
                this.Template.Execute(delegate(ISession session)
                {
                    var sql = session.GetNamedQuery("GetEquipmentState").QueryString.Replace("#tablename", EquipmentReportName(time));
                    sql = sql.Replace("#config", ConditionSQL(t, c));
                    list = session.CreateSQLQuery(sql)
                        .SetInt32("size", size)
                        .SetInt32("index", index)
                        .SetResultTransformer(Transformers.AliasToEntityMap).List();
                    return list;
                });
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        //获取查询条件
        private string ConditionSQL(string k,string c)
        {
            string sql = "";
            switch (k)
            {
                case "1": {
                    sql=Session.GetNamedQuery("Equipment_Condition_ECode").QueryString.Replace("#c", c);
                    break;
                }
                case "2":
                    {
                        sql = Session.GetNamedQuery("Equipment_Condition_Save").QueryString.Replace("#c", c);
                        break;
                    }
                case "3":
                    {
                        sql = Session.GetNamedQuery("Equipment_Condition_EName").QueryString.Replace("#c", c);
                        break;
                    }
                case "4":
                    {
                        sql = Session.GetNamedQuery("Equipment_Condition_EState").QueryString.Replace("#c", c);
                        break;
                    }
                case "5":
                    {
                        sql = Session.GetNamedQuery("Equipment_Condition_Create").QueryString.Replace("#c", c);
                        break;
                    }
                case "6":
                    {
                        sql = Session.GetNamedQuery("Equipment_Condition_Location").QueryString.Replace("#c", c);
                        break;
                    }
            }
            return sql;
        }
        #endregion
    }
}
